package com.zengguang.testkaoshi.controller;

import com.zengguang.testkaoshi.pojo.StudentInfo;
import com.zengguang.testkaoshi.services.impl.StudentInfoService;
import com.zengguang.testkaoshi.tool.ResponseMessage;
import com.zengguang.testkaoshi.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *对外提供数据服务接口
 */
@Api(tags = "studentInfo")
@RestController
@CrossOrigin
public class studentInfoAction {
    @Autowired
    private StudentInfoService studentInfoService;

    @ApiOperation(value = "学生添加方法")
    @RequestMapping(value = "/addstudentInfo",method = RequestMethod.GET)
    public ResponseMessage add(StudentInfo studentInfo) {
        int isAdd = 0;
        //传入数据的判断
        //(1)实体对象不能为空
        if (studentInfo != null) {
                //添加角色数据
                isAdd = studentInfoService.insertStudentInfo(studentInfo);
                if (isAdd == 1) {
                    return Result.success("添加成功");
                } else {
                    return Result.error("添加失败");
                }
        }
        return Result.error("添加失败");
    }

    @ApiOperation(value = "学生修改方法")
    @RequestMapping(value = "/updatestudentInfo",method = RequestMethod.GET)
    public ResponseMessage update(StudentInfo studentInfo) {
        int isUpadte = 0;
        //传入数据的判断
        //(1)实体对象不能为空
        if (studentInfo != null) {

                //添加角色数据
                isUpadte = studentInfoService.updateStudent(studentInfo);
                if (isUpadte >0) {
                    return Result.success("学生信息更新成功");
                } else {
                    return Result.error("学生信息更新失败");
                }

        }
        return Result.error("学生信息更新失败");
    }

    @ApiOperation(value = "学生信息查询方法")
    @RequestMapping(value = "/selectAllstudentInfo",method = RequestMethod.GET)
    public ResponseMessage select(StudentInfo studentInfo) {
        Map<String, List> map=new HashMap<>();
        List<StudentInfo> list=studentInfoService.selectAllStudent();
        if (!list.isEmpty()){
            map.put("list",list);
            return Result.success("200",map);
        }
        return Result.error("查询失败");
    }

    @ApiOperation("删除学生信息")
    @RequestMapping(value = "/deleteStudentInfo",method = RequestMethod.GET)
    public ResponseMessage deleteStudent(int studentinfoid){
        int temp=0;
        if (studentinfoid!=0){
                temp=studentInfoService.deleteStudent(studentinfoid);
                if (temp>0) {
                    return Result.success("删除学生信息成功",temp);
                }
                else {
                    return Result.error("删除学生信息失败");
                }
            }

        return Result.error("学生信息编号不为0");
    }


}


